package com.example.isoftservice.controller;

import cn.hutool.crypto.SecureUtil;
import com.example.isoftservice.advice.ExceptionAdvice;
import com.example.isoftservice.mapper.UserMapper;
import com.example.isoftservice.pojos.User;
import com.example.isoftservice.service.UserService;
import com.example.isoftservice.util.Result;
import com.example.isoftservice.util.logs.HoneyLogs;
import com.example.isoftservice.util.logs.LogTypes;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    //条件查询用户列表
    @RequestMapping(value = "/userList", method = RequestMethod.GET)
    public Result<Map<String,Object>> userList(User user,
                                               @RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo,
                                               @RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize) {
        Result<Map<String,Object>> result = new Result<>();
        result.setCode(0);
        result.setMessage("查询成功");
        result.setSuccess(true);
        result.setResult(userService.searchUserList(user,pageNo,pageSize));
        return result;
    }

    //按id查询用户信息
    @RequestMapping(value = "/searchUserById",method = RequestMethod.GET)
    public Result<User> searchUserById(Integer userId) {
        Result<User> result = new Result<>();
        User user = userService.searchUserById(userId);
        if(user != null) {
            result.setCode(0);
            result.setSuccess(true);
            result.setMessage("查询成功!!!");
            result.setResult(user);
            return result;
        }
        result.setCode(1);
        result.setSuccess(false);
        result.setMessage("用户不存在!");
        return result;
    }

    //登录功能
    @HoneyLogs(operation = "登录",type = LogTypes.LOGIN)
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Result<Map<String,Object>> login(@Valid @RequestBody User user) {
        Result<Map<String, Object>> result = new Result<>();
        result.setResult(userService.login(user.getUsername(),user.getPassword()));
        return result;
    }


    //通过单个或者多个id删除用户
    @RequestMapping("/deleteUserByIds")
    public Result<Boolean> deleteUserByIds(@RequestBody User user) {
        Result<Boolean> result = new Result<>();
        Boolean flag = userService.removeUserByIds(user.ids);
        result.setResult(flag);
        result.setCode(0);
        result.setMessage("删除成功!");
        return result;
    }


    //添加user
    @RequestMapping("/addUser")
    public Result<Boolean> addUser(@RequestBody @Validated User user) {
        Result<Boolean> result = new Result<>();
        //判断用户名是否存在
        if (userService.getUser(user.getUsername()) != null) {
            result.setMessage(ExceptionAdvice.repeatUserName());
            result.setResult(false);
            result.setSuccess(false);
            result.setCode(1);
            return result;
        }
        Boolean flag = userService.addUser(user);
        result.setSuccess(flag);
        result.setResult(flag);
        result.setCode(0);
        result.setMessage("新增成功");
        return result;
    }

    //修改用户
    @RequestMapping("/modifyUserById")
    public Result<Boolean> modifyUserById(@RequestBody User user) {
        Result<Boolean> result = new Result<>();
        result.setMessage("修改成功");
        result.setCode(0);
        Boolean flag = userService.modifyUserById(user);
        result.setResult(flag);
        result.setSuccess(flag);
        return result;
    }

    //修改用户密码
    @RequestMapping(value = "/modifyUserPassword" , method = RequestMethod.POST)
    public Result<Boolean> modifyUserPassword(@RequestBody User user) {
        Result<Boolean> result = new Result<>();
        boolean flag = userService.modifyUserPassword(user);
        result.setResult(flag);
        result.setSuccess(flag);
        result.setMessage("密码修改成功");
        result.setCode(0);
        return result;
    }

    //退出登录
    @RequestMapping("/logout")
    public Result<Boolean> logout() {
        Result<Boolean> result = new Result<>();
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        result.setMessage("退出成功!!!");
        result.setResult(true);
        return result;
    }
}
